package com.sunny.ynhz.ui.ynts.partorlstaticfragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.AreaDBAdapter;
import com.sunny.ynhz.adapter.TimeSelectAdapter;
import com.sunny.ynhz.bean.StandardBean;
import com.sunny.ynhz.bean.TimeBean;
import com.sunny.ynhz.bean.WarnHorBarBean;
import com.sunny.ynhz.model.IProblemTypeResultView;
import com.sunny.ynhz.model.ProblemStandModel;
import com.sunny.ynhz.ui.base.BaseFragment;
import com.sunny.ynhz.utils.ChartUtils;
import com.sunny.ynhz.utils.MoveToPosition;
import com.sunny.ynhz.utils.ShowUtils;
import com.sunny.ynhz.utils.TimeUtils;
import com.sunny.ynhz.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by huhuan123 on 2018/8/8.
 *
 * @describe TODO
 */

public class TownsStaticFragment extends BaseFragment implements IProblemTypeResultView<List<StandardBean.DateBean>> {
    @BindView(R.id.tv_current_week)
    TextView tv_current_week;
    @BindView(R.id.lv_recycler)
    RecyclerView lv_recycler;
    @BindView(R.id.hor_bar_chart)
    HorizontalBarChart hor_bar_chart;
    @BindView(R.id.rl_db)
    RecyclerView rl_db;
    List<String> mKeyList = new ArrayList<>();
    List<List<String>> mcontentList = new ArrayList<>();
    List<TimeBean> list = new ArrayList<>();
    List<StandardBean.DateBean> mstandardBean = new ArrayList<>();
    private int currentKey = 2;
    private int currentContent = 0;
    private LinearLayoutManager linearLayoutManager;
    private TimeSelectAdapter timeSelectAdapter;
    private ProblemStandModel problemStandModel;
    private AreaDBAdapter areaDBAdapter;

    public static TownsStaticFragment getInstanceTownsStaticFragment() {
        return new TownsStaticFragment();
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.city_fragment;
    }

    @Override
    protected void setUpView() {
        super.setUpView();
        tv_current_week.setText(TimeUtils.getSysYear() + "年" + TimeUtils.getCurrentMonth() + "月");
        initData();
        currentContent = TimeUtils.getCurrentMonth() - 1;

        initCurrentDay(TimeUtils.getSysYear(), TimeUtils.getCurrentMonth() + "");
        linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        timeSelectAdapter = new TimeSelectAdapter(list, getContext(), 5);
        lv_recycler.setLayoutManager(linearLayoutManager);
        lv_recycler.setAdapter(timeSelectAdapter);
        MoveToPosition.moveToPositionHor(linearLayoutManager, lv_recycler, currentContent);
        problemStandModel = new ProblemStandModel(this, getContext());
        problemStandModel.getStandData(TimeUtils.getSysYear() + "-" +TimeUtils.getCurrentMonth(), "2");
        areaDBAdapter = new AreaDBAdapter(mstandardBean);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rl_db.setLayoutManager(linearLayoutManager);
        rl_db.setAdapter(areaDBAdapter);
        timeSelectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < list.size(); i++) {
                    TimeBean timeBean = list.get(i);
                    if (i == position) {
                        timeBean.setCheck(true);
                    } else {
                        timeBean.setCheck(false);
                    }
                }
                currentContent = position;
                tv_current_week.setText(mKeyList.get(currentKey) + "年" + list.get(position).getTime() + "月");
                timeSelectAdapter.notifyDataSetChanged();

                problemStandModel.getStandData(mKeyList.get(currentKey) + "-" + list.get(position).getTime(), "2");
            }
        });


    }

    @OnClick({R.id.tv_current_week})
    public void onClickView(View view) {
        displayTimeSelect();
    }

    public void displayTimeSelect() {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(getContext(), new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                currentKey = options1;
                currentContent = option2;
                String mkey = mKeyList.get(currentKey);
                String mContent = mcontentList.get(options1).get(option2);
                tv_current_week.setText(mkey + "年" + mContent + "月");
                initCurrentDay(mkey, mContent);
                timeSelectAdapter.notifyDataSetChanged();
                MoveToPosition.moveToPositionHor(linearLayoutManager, lv_recycler, currentContent);
                problemStandModel.getStandData(mkey + "-" + mContent, "2");

            }
        }).isRestoreItem(true).build();
        pvOptions.setSelectOptions(currentKey, currentContent);
        pvOptions.setPicker(mKeyList, mcontentList);
        pvOptions.show();
    }

    public void initData() {
        mKeyList.add((Integer.parseInt(TimeUtils.getSysYear()) - 2) + "");
        mKeyList.add((Integer.parseInt(TimeUtils.getSysYear()) - 1) + "");
        mKeyList.add(TimeUtils.getSysYear());
        for (int i = 0; i < 3; i++) {
            List<String> mList = new ArrayList<>();
            for (int j = 1; j <= TimeUtils.getCurrentMonth(); j++) {
                mList.add(j + "");
            }
            mcontentList.add(mList);
        }
    }


    private void initCurrentDay(String year, String week) {
        list.clear();
        int maxWeekNumOfYear = TimeUtils.getCurrentMonth();
        for (int i = 1; i <= 12; i++) {
            TimeBean timeBean = new TimeBean();
            if (i == Integer.parseInt(week)) {
                timeBean.setCheck(true);
            } else {
                timeBean.setCheck(false);
            }
            if (year.compareTo(TimeUtils.getSysYear()) == 0 && i > maxWeekNumOfYear) {
                timeBean.setExit(false);
            } else {
                timeBean.setExit(true);
            }

            timeBean.setTime(i + "");
            list.add(timeBean);
        }

    }


    @Override
    public void onSuccess(List<StandardBean.DateBean> dateBeans) {
        loadSuccess();
        ShowUtils.closePopwindow();
        mstandardBean.clear();
        WarnHorBarBean warnData = getWarnData(dateBeans);
        ChartUtils.initHorBar(hor_bar_chart, warnData.getDabiaoList(), warnData.getCityNameList());
        mstandardBean.addAll(dateBeans);
        areaDBAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCache() {

    }

    @Override
    public void onFail() {
        if (mstandardBean.size() > 0) {
            ShowUtils.closePopwindow();
        } else {
            loadFail();
        }


    }

    @Override
    public void onStartLoad() {
        if (mstandardBean.size() > 0) {
            ShowUtils.inItPopwin(getContext(), "请等待");
        } else {
            startLoad();
        }


    }

    @Override
    public void resLoad() {
        super.resLoad();
        problemStandModel.getStandData(TimeUtils.getSysYear() + "-" + TimeUtils.getCurrentMonth(), "2");
    }

    public WarnHorBarBean getWarnData(List<StandardBean.DateBean> dateBeans) {
        WarnHorBarBean warnHorBarBean = new WarnHorBarBean();
        ArrayList<String> mlist = new ArrayList<>();
        ArrayList<Float> mDaBiaoList = new ArrayList<>();
        for (int i = 0; i < dateBeans.size(); i++) {
            mlist.add(dateBeans.get(i).getADDVNM());
            mDaBiaoList.add(dateBeans.get(i).getVILLAGE_PERCENT());

        }
        warnHorBarBean.setCityNameList(mlist);
        warnHorBarBean.setDabiaoList(mDaBiaoList);
        return warnHorBarBean;
    }
}

